﻿using PhotoKiosk.Model;
using PhotoKiosk.ViewModel.Helpers;
using PhotoKiosk.ViewModel.UserControls;
using System;
using System.Collections.Generic;
using System.Windows.Input;

namespace PhotoKiosk.ViewModel
{
    public class PaperSelectViewModel : ViewModelBase
    {
        private DelegateCommand m_finishCommand;
        private readonly MainWindowViewModel m_mainWindowViewModel;
        private bool isPaperFrameSelected;
        private bool isPaperSizeSelected;
        private bool isPaperTypeSelected;

        public PaperSelectViewModel(MainWindowViewModel i_mainWindowViewModel)
        {
            m_mainWindowViewModel = i_mainWindowViewModel;
            isPaperFrameSelected = false;
            isPaperSizeSelected = false;
            isPaperTypeSelected = false;
        }

        public ICommand FinishCommand
        {
            get
            {
                if (m_finishCommand == null)
                {
                    m_finishCommand = new DelegateCommand(param => this.finishCommandExecute());
                }
                return m_finishCommand;
            }
        }

        private void finishCommandExecute()
        {
            if (checkValidation())
            {
                m_mainWindowViewModel.CurrentViewModel = new ProcessingViewModel(m_mainWindowViewModel, () => LoadPhotos(), m_mainWindowViewModel.ResourceManager.GetString("While_Loading_Your_Images_To_The_Computer"));
            }
        }

        public void LoadPhotos()
        {
            m_mainWindowViewModel.CurrentViewModel = new PhotosSelectViewModel(m_mainWindowViewModel);
        }

        private List<PaperSizeViewModel> m_paperSizeViewModels;
        public List<PaperSizeViewModel> PaperSizeViewModels
        {
            get
            {
                if (m_paperSizeViewModels == null)
                {
                    m_paperSizeViewModels = new List<PaperSizeViewModel>();
                    foreach (PaperSize ps in m_mainWindowViewModel.SystemData.PaperSizes)
                    {
                        PaperSizeViewModel psvm = new PaperSizeViewModel(ps);
                        psvm.OnClick += onPaperSizeSelected;
                        m_paperSizeViewModels.Add(psvm);
                    }
                }
                return m_paperSizeViewModels;
            }
        }

        private List<PaperFrameButtonViewModel> m_paperFramesViewModels;
        public List<PaperFrameButtonViewModel> PaperFramesViewModels
        {
            get
            {
                if (m_paperFramesViewModels == null)
                {
                    m_paperFramesViewModels = new List<PaperFrameButtonViewModel>();
                    foreach (PaperFrame pf in (PaperFrame[])Enum.GetValues(typeof(PaperFrame)))
                    {
                        PaperFrameButtonViewModel dgbvm = new PaperFrameButtonViewModel(pf);
                        dgbvm.OnClick += onPaperFrameSelected;
                        m_paperFramesViewModels.Add(dgbvm);
                    }
                }
                return m_paperFramesViewModels;
            }
        }

        private List<PaperTypeButtonViewModel> m_paperTypesViewModels;
        public List<PaperTypeButtonViewModel> PaperTypesViewModels
        {
            get
            {
                if (m_paperTypesViewModels == null)
                {
                    m_paperTypesViewModels = new List<PaperTypeButtonViewModel>();
                    foreach (PaperType pt in (PaperType[])Enum.GetValues(typeof(PaperType)))
                    {
                        PaperTypeButtonViewModel dgbvm = new PaperTypeButtonViewModel(pt);
                        dgbvm.OnClick += onPaperTypeSelected;
                        m_paperTypesViewModels.Add(dgbvm);
                    }
                }
                return m_paperTypesViewModels;
            }
        }

        private bool m_isPaperFrameSelectedErrorColor = true;
        public bool IsPaperFrameSelectedErrorColor
        {
            get
            {
                return m_isPaperFrameSelectedErrorColor;
            }
            set
            {
                m_isPaperFrameSelectedErrorColor = value;
                OnPropertyChanged("IsPaperFrameSelectedErrorColor");
            }
        }

        private bool m_isPaperSizeSelectedErrorColor = true;
        public bool IsPaperSizeSelectedErrorColor
        {
            get
            {
                return m_isPaperSizeSelectedErrorColor;
            }
            set
            {
                m_isPaperSizeSelectedErrorColor = value;
                OnPropertyChanged("IsPaperSizeSelectedErrorColor");
            }
        }

        private bool m_isPaperTypeSelectedErrorColor = true;
        public bool IsPaperTypeSelectedErrorColor
        {
            get
            {
                return m_isPaperTypeSelectedErrorColor;
            }
            set
            {
                m_isPaperTypeSelectedErrorColor = value;
                OnPropertyChanged("IsPaperTypeSelectedErrorColor");
            }
        }

        private bool checkValidation()
        {
            bool result = true;
            IsPaperFrameSelectedErrorColor = true;
            IsPaperSizeSelectedErrorColor = true;
            IsPaperTypeSelectedErrorColor = true;
            if(isPaperFrameSelected == false)
            {
                IsPaperFrameSelectedErrorColor = false;
                result = false;
            }
            if(isPaperSizeSelected == false)
            {
                IsPaperSizeSelectedErrorColor = false;
                result = false;
            }
            if (isPaperTypeSelected == false)
            {
                IsPaperTypeSelectedErrorColor = false;
                result = false;
            }
            return result;
        }

        private void onPaperTypeSelected(PaperType i_paperType)
        {
            foreach (PaperTypeButtonViewModel ptvm in PaperTypesViewModels)
            {
                ptvm.IsSelected = false;
            }
            isPaperTypeSelected = true;
            IsPaperTypeSelectedErrorColor = true;
            m_mainWindowViewModel.SystemData.Order.PaperType = i_paperType;
        }

        private void onPaperFrameSelected(PaperFrame i_paperFrame)
        {
            foreach (PaperFrameButtonViewModel pfvm in PaperFramesViewModels)
            {
                pfvm.IsSelected = false;
            }
            isPaperFrameSelected = true;
            IsPaperFrameSelectedErrorColor = true;
            m_mainWindowViewModel.SystemData.Order.PaperFrame = i_paperFrame;
        }

        private void onPaperSizeSelected(PaperSize i_paperSize)
        {
            foreach (PaperSizeViewModel psvm in PaperSizeViewModels)
            {
                psvm.IsSelected = false;
            }
            isPaperSizeSelected = true;
            IsPaperSizeSelectedErrorColor = true;
            m_mainWindowViewModel.SystemData.Order.PaperSize = i_paperSize;
        }
    }
}
